import { Component, OnInit, ViewChild, OnDestroy, AfterContentInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { NzMessageService, UploadFile, NzModalService, NzNotificationService } from 'ng-zorro-antd';
import { catchError } from 'rxjs/operators';
import { of } from 'rxjs';
import { ActivatedRoute, Router } from '@angular/router';
import {SettingsService, TitleService} from '@delon/theme';
import { Constant, ServiceResult } from '@core/constant';
import { maService } from '../../../../service/ma/ma.service';
import { EmitterService } from 'app/service/emit.service';
import { CommService } from '../../../../service/comm/comm.service';
import {insService} from "../../../../service/ins/ins.service";
import {ProductService} from "../../../../service/prod/product.service";
import {SimpleTableColumn, SimpleTableComponent} from "@delon/abc";
import {spListService} from "../../../../service/spList/spList.service";

@Component({
  selector: 'batchplan-detail-edit',
  templateUrl: './batchplan-detail.component.html',
  styleUrls: ['./batchplan-detail.component.less']
})
export class batchplandetailViewEditComponent implements OnInit, OnDestroy {


  checkedList = [];
  uploadExcelUrl = this.spListService.uploadpcplan();
  downurl='';

  @ViewChild('st') st: SimpleTableComponent;
  columns: SimpleTableColumn[] = [
    // { title: '', index: 'key', type: 'checkbox',exported:false },
    { title: '采购组', index: 'ejgrp' },
    { title: '供应商', index: 'lifnr' },
    { title: '供应商名称', index: 'name1' },
    { title: '序号', index: 'xuhao' },

    { title: '采购订单编号', index: 'ebeln' },
    { title: '采购订单项目', index: 'ebelp' },
    { title: '物料编码', index: 'matnr' },
    { title: '物料描述', index: 'maktx' },
    { title: '单位', index: 'meins' },
    { title: '供应商物料编码', index: 'idnlf' },

    { title: '交货日期', index: 'eindt' },
    { title: '回复交期', index: 'eindthf' },
    { title: '订单数量', index: 'menge' },
    // { title: '未清数量', index: 'menge1' },
    { title: '批次数量', index: 'menge2' },
    { title: '回复备注', index: 'hfremark' },
    { title: '单据类型', index: 'zzph' },
    { title: '库存类型', index: 'zwxdkc' },
    { title: '是否备料', index: 'zsfbl' }


  ];
  loading = false;
  constructor(
    private productService: ProductService,

    public setting: SettingsService,
    private msg: NzMessageService,
    private modalService: NzModalService,
    private maService: maService,
    private emit: EmitterService,
    private notice: NzNotificationService,
    private route: ActivatedRoute,
    private titleService: TitleService,
    private cnt: Constant,
    private router: Router,
    private comm: CommService,
    private spListService: spListService,

  ) {}
  ps=10;
  q: any = {
    lifnr:'',
    ebeln: '',
    ebelp: '',
    matnr:'',
    maktx:'',
    vbeln:'',
    vbeln1:''
  };
  private sub: any;
url="";
  ngOnInit(): void {
    // 获取参数
    this.sub = this.route.params.subscribe(params => {
      this.q.ebeln = params["id"];
        if(this.q.ebeln=='undefined'){
          this.q.ebeln = '';

        } else{
          this.q.ebeln = params["id"];
          this.q.ebelp = params["no"];

        }
      // 获取商品详情
              this.url=this.spListService.listplanbatch(this.q.ebeln);
    });
    // 隐藏reusetab
    // setTimeout(() => {
    //   this.emit.sendMessage("HIDE_REUSE_TAB");
    // }, 100);
    // this.titleService.setTitle("批次显示")

  }

  // 显示reusetab
  ngOnDestroy() {
    this.emit.sendMessage("SHOW_REUSE_TAB");
  }
  back() {
    this.router.navigate(['/poList/poList']);
  }
  checkboxChange(list: any[]) {
    this.checkedList = list;
  }

  loaddata(wqtype){
    // console.log(wqtype);
    // this.q.bpmStatus=wqtype;
    this.st.load(1,this.q)
  }
  packatta(){
    this.loading = true;

    let conmapList = [];

    try{
      let dto1 ={
        conKey:"ebeln",
        conValue:this.q.ebeln,
      }
      conmapList.push(dto1);

      let dto2 ={
        conKey:"matnr",
        conValue:this.q.matnr,
      }
      conmapList.push(dto2);

      let dto3 ={
        conKey:"lifnr",
        conValue:this.q.lifnr,
      }
      conmapList.push(dto3);
    }catch (e) {

    }
    let  conhead={
      contempPath:"https://zhaodui.oss-cn-shenzhen.aliyuncs.com/doc/htmb/hxhtmb.docx",
      conmapList:JSON.parse(JSON.stringify(conmapList)),
    }
    this.comm.genbatchplanwq(this.q.ebeln,conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;

        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })
  }
  opennewwindow(url){

    const newWindow = window.open(url, '_blank');
    newWindow.document.title = '下载';

  }

  uploadProd(info: { file: UploadFile }) {
    if (info.file.status === 'uploading') {
      this.loading = true;
    }
    if (info.file.status === 'done') {
      this.loading = false;
      console.log("info1*********"+JSON.stringify(info.file.response[0]));
      this.st.load();
      if(info.file.response[0].piccontent==='success'){
        this.notice.create('success', '成功', info.file.response[0].piccontent);
      }else{
        this.notice.create('error', '失败', info.file.response[0].piccontent);
      }
    }
    if (info.file.status === 'error') {
      this.loading = false;
      this.notice.create('error', '失败', '导入失败');
    }
  }

}
